package cn.edu.zjicm.wordsnet_d.data;

import java.io.Serializable;

import android.content.Context;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference.BookType;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.view.SettingPlanDialog;

public class StudyPlan implements Serializable {

	private int bookType = -1; // 书类别，是单词书还是词组书
	private int studyMode = -1; // 学习模式，是结束时间还是每天学习个数
	private int endYear = -1, endMonth = -1, endDay = -1; // 结束时间的年月日,这是按照结束时间
	private int everyDayNumber = -1; // 每天学习个数
	private int startPlanTime = -1; // 计划开始时间
	private int bookIndex; // 书的index
	private boolean isLearning; // 当前这本书是否在学

	public boolean isLearning() {
		return isLearning;
	}

	public void setLearning(boolean isLearning) {
		this.isLearning = isLearning;
	}

	public int getBookIndex() {
		return bookIndex;
	}

	public void setBookIndex(int bookIndex) {
		this.bookIndex = bookIndex;
	}

	public int getBookType() {
		return bookType;
	}

	public void setBookType(int bookType) {
		this.bookType = bookType;
	}

	public int getStudyMode() {
		return studyMode;
	}

	public void setStudyMode(int studyMode) {
		this.studyMode = studyMode;
	}

	public int getEndYear() {
		return endYear;
	}

	public void setEndYear(int endYear) {
		this.endYear = endYear;
	}

	public int getEndMonth() {
		return endMonth;
	}

	public void setEndMonth(int endMonth) {
		this.endMonth = endMonth;
	}

	public int getEndDay() {
		return endDay;
	}

	public void setEndDay(int endDay) {
		this.endDay = endDay;
	}

	public int getEveryDayNumber() {
		return everyDayNumber;
	}

	public void setEveryDayNumber(int everyDayNumber) {
		this.everyDayNumber = everyDayNumber;
	}

	public int getStartPlanTime() {
		return startPlanTime;
	}

	public void setStartPlanTime(int startPlanTime) {
		this.startPlanTime = startPlanTime;
	}

	/**
	 * 保存计划
	 * 
	 * @param mContext
	 */
	public void savePlan(Context mContext) {
		AppPreference.setFirstTimeStudyNewBook(mContext, true);
		if (bookType == AppPreference.BookType.BOOK_WORD) {

			AppPreference.setWordBookIndex(mContext, bookIndex);
			AppPreference.setWordStudyMode(mContext, studyMode);
			AppPreference.setWordPlanStartDate(mContext, startPlanTime);
			AppPreference.setWordBookIsLearning(mContext, isLearning);

			if (studyMode == AppPreference.StudyMode.MODE_END_TIME) {
				AppPreference.setWordPlanEndYear(mContext, endYear);
				AppPreference.setWordPlanEndMonth(mContext, endMonth);
				AppPreference.setWordPlanEndDay(mContext, endDay);
			} else {
				AppPreference.setWordEveryDayNumber(mContext, everyDayNumber);
			}
		} else {
			AppPreference.setPhraseBookIndex(mContext, bookIndex);
			AppPreference.setPhraseStudyMode(mContext, studyMode);
			AppPreference.setPhrasePlanStartDate(mContext, startPlanTime);
			AppPreference.setPhraseBookIsLearning(mContext, isLearning);

			if (studyMode == AppPreference.StudyMode.MODE_END_TIME) {
				AppPreference.setPhrasePlanEndYear(mContext, endYear);
				AppPreference.setPhrasePlanEndMonth(mContext, endMonth);
				AppPreference.setPhrasePlanEndDay(mContext, endDay);
			} else {
				AppPreference.setPhraseEveryDayNumber(mContext, everyDayNumber);
			}
		}
	}

	/**
	 * 获取单词计划
	 * 
	 * @param context
	 * @return
	 */
	public static StudyPlan getWordStudyPlan(Context context) {
		StudyPlan studyPlan;
		if (AppPreference.getWordBookIndex(context) != -1) {
			studyPlan = new StudyPlan();
			studyPlan.setBookIndex(AppPreference.getWordBookIndex(context));
			studyPlan.setStudyMode(AppPreference.getWordStudyMode(context));
			studyPlan.setStartPlanTime(AppPreference.getWordPlanStartDate(context));
			studyPlan.setLearning(AppPreference.getWordBookIsLearning(context));
			studyPlan.setBookType(BookType.BOOK_WORD);

			if (studyPlan.studyMode == AppPreference.StudyMode.MODE_END_TIME) {
				studyPlan.setEndYear(AppPreference.getWordPlanEndYear(context));
				studyPlan.setEndMonth(AppPreference.getWordPlanEndMonth(context));
				studyPlan.setEndDay(AppPreference.getWordPlanEndDay(context));
			} else {
				studyPlan.setEveryDayNumber(AppPreference.getWordEveryDayNumber(context));
			}
		} else {
			studyPlan = null;
		}
		return studyPlan;
	}

	/**
	 * 获取词组计划
	 * 
	 * @param context
	 * @return
	 */
	public static StudyPlan getPhraseStudyPlan(Context context) {
		StudyPlan studyPlan;
		if (AppPreference.getPhraseBookIndex(context) != -1) {
			studyPlan = new StudyPlan();
			studyPlan.setBookIndex(AppPreference.getPhraseBookIndex(context));
			studyPlan.setStudyMode(AppPreference.getPhraseStudyMode(context));
			studyPlan.setStartPlanTime(AppPreference.getPhrasePlanStartDate(context));
			studyPlan.setLearning(AppPreference.getPhraseBookIsLearning(context));
			studyPlan.setBookType(BookType.BOOK_PHRASE);

			if (studyPlan.studyMode == AppPreference.StudyMode.MODE_END_TIME) {
				studyPlan.setEndYear(AppPreference.getPhrasePlanEndYear(context));
				studyPlan.setEndMonth(AppPreference.getPhrasePlanEndMonth(context));
				studyPlan.setEndDay(AppPreference.getPhrasePlanEndDay(context));
			} else {
				studyPlan.setEveryDayNumber(AppPreference.getPhraseEveryDayNumber(context));
			}
		} else {
			studyPlan = null;
		}

		return studyPlan;
	}

	public static StudyPlan getStudyPlan(Context context, int bookIndex) {
		LogUtil.d("bookIndex=" + bookIndex + ",保存的单词index=" + AppPreference.getWordBookIndex(context) + ",保存的词组index=" + AppPreference.getPhraseBookIndex(context));
		if (bookIndex == AppPreference.getWordBookIndex(context)) {
			LogUtil.d("获取的是单词");
			return getWordStudyPlan(context);
		} else if (bookIndex == AppPreference.getPhraseBookIndex(context)) {
			LogUtil.d("获取的是词组");
			return getPhraseStudyPlan(context);
		} else {
			LogUtil.d("获取的是空");
			return null;
		}
	}

	/**
	 * 清除单词学习计划
	 * 
	 * @param context
	 */
	public static void clearWordStudyPlan(Context context) {
		AppPreference.setWordBookIndex(context, -1);
		AppPreference.setWordBookIsLearning(context, false);
		AppPreference.setWordStudyMode(context, -1);
		AppPreference.setWordEveryDayNumber(context, -1);
		AppPreference.setWordPlanEndDay(context, -1);
		AppPreference.setWordPlanEndMonth(context, -1);
		AppPreference.setWordPlanEndYear(context, -1);
		AppPreference.setWordPlanStartDate(context, 0);
	}

	/**
	 * 清除词组学习计划
	 * 
	 * @param context
	 */
	public static void clearPhraseStudyPlan(Context context) {
		AppPreference.setPhraseBookIndex(context, -1);
		AppPreference.setPhraseBookIsLearning(context, false);
		AppPreference.setPhraseStudyMode(context, -1);
		AppPreference.setPhraseEveryDayNumber(context, -1);
		AppPreference.setPhrasePlanEndDay(context, -1);
		AppPreference.setPhrasePlanEndMonth(context, -1);
		AppPreference.setPhrasePlanEndYear(context, -1);
		AppPreference.setPhrasePlanStartDate(context, 0);
	}

	/**
	 * 获取当前单词的学习模式
	 * 
	 * @param context
	 * @return
	 */
	public static int getWordBookStudyMode(Context context) {
		if (isLearningWordBook(context)) {
			return AppPreference.getWordStudyMode(context);
		}
		return -1;
	}

	/**
	 * 获取当前词组的学习模式
	 * 
	 * @param context
	 * @return
	 */
	public static int getPhraseBookStudyMode(Context context) {
		if (isLearningPhraseBook(context)) {
			return AppPreference.getPhraseStudyMode(context);
		}
		return -1;
	}

	@Override
	public String toString() {
		return "bookIndex=" + bookIndex + ",bookIsLearning=" + isLearning + ",studyMode=" + studyMode + ",everyDayNumber=" + everyDayNumber + ",endYear=" + endYear + ",endMonth=" + endMonth
				+ ",endDay=" + endDay + ",startDate=" + startPlanTime;
	}

	public static boolean isLearningWordBook(Context context) {
		if (AppPreference.getWordBookIndex(context) != -1 && AppPreference.getWordBookIsLearning(context)) {
			return true;
		}
		return false;
	}

	public static boolean isLearningPhraseBook(Context context) {
		if (AppPreference.getPhraseBookIndex(context) != -1 && AppPreference.getPhraseBookIsLearning(context)) {
			return true;
		}
		return false;
	}
}
